package cn.snowheart.command;

import cn.snowheart.command.command.*;
import cn.snowheart.command.inter.CeilingFan;
import cn.snowheart.command.inter.Fan;
import cn.snowheart.command.inter.GarageDoor;
import cn.snowheart.command.inter.Light;

import java.util.stream.IntStream;

/**
 * 测试主类
 *
 * @author liuwanxiang
 * @version 2019/07/04
 */
public class CommandTests {

    public static void main(String[] args) {

        // testSimpleRemoteControl();

        // testRemoteControl();

        // testSupportUndoControlV1();

        testSupportUndoControlV2();
    }

    private static void testSimpleRemoteControl() {
        Light light = new Light("bedroom");
        LightOnCommand command = new LightOnCommand(light);

        SimpleRemoteControl control = new SimpleRemoteControl();
        control.setCommand(command);
        control.onClick();
    }

    private static void testRemoteControl() {
        RemoteControl control = buildRemoteControl();

        IntStream.range(0, 5).forEach(control::onPressed);
        IntStream.range(0, 5).forEach(control::offPressed);
    }

    private static RemoteControl buildRemoteControl() {
        RemoteControl control = new RemoteControl(5);

        // 构建卧室的灯控制命令
        Light bedRoomLight = new Light("bedroom");
        LightOnCommand bedRoomLightOnCommand = new LightOnCommand(bedRoomLight);
        LightOffCommand bedRoomLightOffCommand = new LightOffCommand(bedRoomLight);
        control.setCommand(0, bedRoomLightOnCommand, bedRoomLightOffCommand);

        // 构建浴室的灯控制命令
        Light bathRoomLight = new Light("bathroom");
        LightOnCommand bathRoomLightOnCommand = new LightOnCommand(bathRoomLight);
        LightOffCommand bathRoomLightOffCommand = new LightOffCommand(bathRoomLight);
        control.setCommand(1, bathRoomLightOnCommand, bathRoomLightOffCommand);

        // 车库门的控制命令
        GarageDoor garageDoor = new GarageDoor();
        GarageDoorOpenCommand garageDoorOpenCommand = new GarageDoorOpenCommand(garageDoor);
        GarageDoorCloseCommand garageDoorCloseCommand = new GarageDoorCloseCommand(garageDoor);
        control.setCommand(2, garageDoorOpenCommand, garageDoorCloseCommand);

        // 风扇的控制命令
        Fan fan = new Fan();
        FanStartCommand fanStartCommand = new FanStartCommand(fan);
        FanStopCommand fanStopCommand = new FanStopCommand(fan);
        control.setCommand(3, fanStartCommand, fanStopCommand);

        return control;
    }

    private static void testSupportUndoControlV1() {
        SupportUndoRemoteControlV1 control = buildSupportUndoRemoteControlV1();
        control.onPressed(0);
        control.offPressed(0);
        control.revertPressed();
    }

    private static SupportUndoRemoteControlV1 buildSupportUndoRemoteControlV1() {
        SupportUndoRemoteControlV1 control = new SupportUndoRemoteControlV1(1);

        // 构建卧室的灯控制命令
        Light bedRoomLight = new Light("bedroom");
        LightOnCommand bedRoomLightOnCommand = new LightOnCommand(bedRoomLight);
        LightOffCommand bedRoomLightOffCommand = new LightOffCommand(bedRoomLight);
        control.setCommand(0, bedRoomLightOnCommand, bedRoomLightOffCommand);

        return control;
    }

    private static void testSupportUndoControlV2() {
        SupportUndoRemoteControlV2 control = buildSupportUndoRemoteControlV2();
        control.buttnonPressed(3);
        control.buttnonPressed(1);
        control.revertPressed();
    }

    private static SupportUndoRemoteControlV2 buildSupportUndoRemoteControlV2() {
        SupportUndoRemoteControlV2 control = new SupportUndoRemoteControlV2(4);

        CeilingFan ceilingFan = new CeilingFan("living room");

        control.setCommand(0, new CeilingFanOffCommand(ceilingFan));
        control.setCommand(1, new CeilingFanLowCommand(ceilingFan));
        control.setCommand(2, new CeilingFanMediumCommand(ceilingFan));
        control.setCommand(3, new CeilingFanHighCommand(ceilingFan));

        return control;
    }

}
